//
//  CJBannerAdapter.m
//  Pods
//
//  Created by ly on 2024/3/4.
//

#import "CJBannerAdapter.h"
#import "CJAdapterHeader.h"
#import "CJBannerCustomEvent.h"
#import "CJATTMBiddingManager.h"
#import "CJCommonTool.h"
#import "CJAdapterSetup.h"

@interface CJBannerAdapter()

@property (nonatomic, strong) CJBannerCustomEvent *customEvent;
@property (nonatomic, strong) CJBannerAd *bannerAd;

@end
@implementation CJBannerAdapter

- (instancetype)initWithNetworkCustomInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo {
    if (self = [super init]) {
//        [CJAdapterSetup setupCJSDKWithAppId:@"appid"];
    }
    return self;
}

- (void)loadADWithInfo:(NSDictionary*)serverInfo localInfo:(NSDictionary*)localInfo completion:(void (^)(NSArray<NSDictionary *> *, NSError *))completion {

    CGSize adSize = CGSizeMake(320.0f, 50.0f);
    CGRect frame = CGRectMake(0, 0, adSize.width, adSize.height);


    _customEvent = [[CJBannerCustomEvent alloc] initWithInfo:serverInfo localInfo:localInfo];
    _customEvent.requestCompletionBlock = completion;
    CJATTMBiddingRequest *request = [[CJATTMBiddingManager sharedInstance] getRequestItemWithUnitID:serverInfo[@"unitid"]];
    if (request) {
        if (request.customObject != nil) { // load secced
            self.bannerAd = request.customObject;
            self.bannerAd.delegate = _customEvent;
            [_customEvent trackBannerAdLoaded:request.bannerView adExtra:@{}];
        } else { // fail
            NSError *error = [NSError errorWithDomain:ATADLoadingErrorDomain code:ATAdErrorCodeThirdPartySDKNotImportedProperly userInfo:@{NSLocalizedDescriptionKey:@"AT has failed to load native.", NSLocalizedFailureReasonErrorKey:@"It took too long to load placement stragety."}];
            [_customEvent trackBannerAdLoadFailed:error];
        }
        [[CJATTMBiddingManager sharedInstance] removeRequestItmeWithUnitID:serverInfo[@"unitid"]];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.bannerAd = [[CJBannerAd alloc] initWithResourceId:serverInfo[@"unitid"] rootViewController:[ATBannerCustomEvent rootViewControllerWithPlacementID:((ATPlacementModel*)serverInfo[kATAdapterCustomInfoPlacementModelKey]).placementID requestID:serverInfo[kATAdapterCustomInfoRequestIDKey]] rect:frame];
            self.bannerAd.delegate = self.customEvent;
            [self.bannerAd loadAdData];
        });
    }
}

+ (void)showBanner:(ATBanner *)banner inView:(UIView *)view presentingViewController:(UIViewController* )viewController {
    //展示banner
    
}

+ (void)bidRequestWithPlacementModel:(ATPlacementModel *)placementModel unitGroupModel:(ATUnitGroupModel *)unitGroupModel info:(NSDictionary *)info completion:(void(^)(ATBidInfo *bidInfo, NSError *error))completion {
    if (NSClassFromString(@"CJADManager") == nil) {
       if (completion != nil) { completion( nil, [NSError errorWithDomain:@"com.ubix.mediation.ios" code:1 userInfo:@{NSLocalizedDescriptionKey:@"Bid request has failed", NSLocalizedFailureReasonErrorKey:@"CJAD is not imported"}]); }
       return;
    }
    [CJADManager configure:info[@"appid"]];
    CJATTMBiddingManager *biddingManage = [CJATTMBiddingManager sharedInstance];
    CJATTMBiddingRequest *request = [CJATTMBiddingRequest new];
    request.unitGroup = unitGroupModel;
    request.placementID = placementModel.placementID;
    request.bidCompletion = completion;
    request.unitID = info[@"unitid"];
    request.extraInfo = info;
    request.adType = ESCAdFormatBanner;
    CGRect frame = CGRectMake(0, 0, unitGroupModel.adSize.width, unitGroupModel.adSize.height);
    dispatch_async(dispatch_get_main_queue(), ^{
        CJBannerAd *bannerAd = [[CJBannerAd alloc] initWithResourceId:info[@"unitid"] rootViewController:[CJCommonTool currentRootController] rect:frame];
        request.customObject = bannerAd;
        [biddingManage startWithRequestItem:request];
        [bannerAd loadAdData];
    });
}

+ (void)sendWinnerNotifyWithCustomObject:(id)customObject secondPrice:(NSString*)price userInfo:(NSDictionary<NSString *, NSString *> *)userInfo {
    CJSplashAd *splashAd = (CJSplashAd *)customObject;
    NSLog(@"比价成功");
}

+ (void)sendLossNotifyWithCustomObject:(nonnull id)customObject lossType:(ATBiddingLossType)lossType winPrice:(nonnull NSString *)price userInfo:(NSDictionary *)userInfo {
    NSLog(@"比价失败");
}

@end
